package com.zy.qudadid.presenter;

import com.zy.qudadid.model.Res;
import com.zy.qudadid.model.StrokeInfor;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.view.IndexSView;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ninos on 2016/9/19.
 */
public class IndexSPresenter extends BasePresenterImp<IndexSView> {
    public void MyTravel(){
        Map mapTravel = new HashMap();
        mapTravel.put("user_id", new UserUtil(view.getContext()).getUserId());
        mapTravel.put("type", "1");
        mapTravel.put("nowpage", "1");
        mapTravel.put("page_num", "4");
        mapTravel.put("finished", "2");
        mapTravel.put("car_type", "2");
        String d = "";
        String jsonTravel = StringUtil.toJson(mapTravel);
        try {
            d = AESCrypt.getInstance().encrypt(jsonTravel);
        } catch (Exception ex) {
        }

        Subscription subscription = Net.getService()
                .MyTravel(d)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<ArrayList<StrokeInfor>>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.error();
                    }

                    @Override
                    public void onNext(Res<ArrayList<StrokeInfor>> res) {
                        if (res.code == Const.OK) {
                            view.success(res.datas);
                        } else {
                        }
                    }
                });

        addSubscription(subscription);
    }

    public void MyCommonTravel(String data){
        Subscription subscription = Net.getService()
                .MyTravel(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<ArrayList<StrokeInfor>>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.errorCommon();
                    }

                    @Override
                    public void onNext(Res<ArrayList<StrokeInfor>> res) {
                        if (res.code == Const.OK) {
                            view.successCommon(res.datas);
                        } else {
                        }
                    }
                });

        addSubscription(subscription);
    }
}
